package com.lanou.musicproject.tools;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.MusicPlayActivity;
import com.lanou.musicproject.adapter.PopupAdapter;
import com.lanou.musicproject.bean.MyMusicList;

import java.util.ArrayList;

/**
 * Created by dllo on 15/11/5.
 */
public class MusicPlayPopupWindow extends PopupWindow {
    private ListView popup_listView;
    private View mMenuView;
    private ImageView Imag_cancel;
    private PopupAdapter adapter;

    public MusicPlayPopupWindow( Context mContext, ArrayList<MyMusicList> myMusicLists, View.OnClickListener itemOnClick) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
                (MusicPlayActivity.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popup_listview,null);
        popup_listView = (ListView) mMenuView.findViewById(R.id.popup_listView);
        Imag_cancel = (ImageView) mMenuView.findViewById(R.id.Imag_cancel);
        adapter = new PopupAdapter(mContext,myMusicLists);
        Log.i("+++++++++>>>>>>",myMusicLists+"++++++++");
        popup_listView.setAdapter(adapter);
        Imag_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //设置按钮监听
        popup_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //播放方法
                //


            }
        });
        //设置MusicPlayPopupWindow
        this.setContentView(mMenuView);
        //设置MusicPlayPopupWindow弹出窗体宽和高
        this.setWidth(600);
        this.setHeight(720);
        //设置MusicPlayPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置MusicPlayPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.album_item);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xbf000000);
        //设置MusicPlayPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.Imag_cancel).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP){
                    if (y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });

    }


}
